﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

using PortalIntranet.DatabaseComponents.Comun;
using System.Data.Entity.ModelConfiguration;
using PortalIntranet.DatabaseComponents.Seguranca;

namespace PortalIntranet.DatabaseComponents.Administracao
{
	[Serializable]
	[Table("Departamentos")]
	public class Departamento : Entidade
	{
		public Departamento()
		{
			this.SubDepartamentos = new HashSet<Departamento>();
			this.WebPages = new HashSet<WebPage>();
			this.Usuarios = new HashSet<Usuario>();
		}

		[Required, MaxLength(50)]
		public string Nome { get; set; }

		[Required, MaxLength(50)]
		public string Slug { get; set; }

		public int Ordem { get; set; }

		public bool Home { get; set; }

		#region Empresa
		public int EmpresaId { get; set; }

		public virtual Empresa Empresa { get; set; }
		#endregion

		#region DepartamentoPai
		public int? DepartamentoPaiId { get; set; }

		public virtual Departamento DepartamentoPai { get; set; }
		#endregion

		#region Navegação
		public virtual ICollection<Departamento> SubDepartamentos { get; set; }

		public virtual ICollection<WebPage> WebPages { get; set; }

		public virtual ICollection<Usuario> Usuarios { get; set; }
		#endregion
	}

	internal class DepartamentoConfiguration : EntityTypeConfiguration<Departamento>
	{
		internal DepartamentoConfiguration()
		{
			this.HasRequired(b => b.Empresa)
				.WithMany(i => i.Departamentos)
				.HasForeignKey(b => b.EmpresaId);

			this.HasOptional(i => i.DepartamentoPai)
				.WithMany(u => u.SubDepartamentos)
				.HasForeignKey(i => i.DepartamentoPaiId);
		}
	}
}
